<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,200分)- 学生方阵（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>学校组织活动&#xff0c;将学生排成一个矩形方阵。</p> 
<p>请在矩形方阵中找到最大的位置相连的男生数量。</p> 
<p>这个相连位置在一个直线上&#xff0c;方向可以是水平的&#xff0c;垂直的&#xff0c;成对角线的或者呈反对角线的。</p> 
<p>注&#xff1a;学生个数不会超过10000</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入的第一行为矩阵的行数和列数&#xff0c;接下来的n行为矩阵元素&#xff0c;元素间用”,”分隔。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出一个整数&#xff0c;表示矩阵中最长的位置相连的男生个数。</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">3,4<br /> F,M,M,F<br /> F,M,M,F<br /> F,F,F,M</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">3</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;"><img alt="" height="167" src="https://img-blog.csdnimg.cn/0c7a268e639c4f41a184c560483fa916.png" width="266" /></td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题的解题思路其实不难&#xff0c;遍历查找矩阵中每一个M点&#xff0c;然后求该M点的水平、垂直、正对角线、反对角线&#xff0c;四个方向的M点个数&#xff0c;然后保留最大的个数&#xff0c;就是题解。</p> 
<p></p> 
<p>但是这种方法会存在很多重复的查找&#xff0c;比如</p> 
<p><img alt="" height="342" src="https://img-blog.csdnimg.cn/3caa1df5378d40ab9c39696da8b2d6b0.png" width="252" /></p> 
<p>红色M是当前遍历到的M&#xff0c;绿色M是以红色M为原点查找到的M&#xff0c;如上图两个红色M点会重复查找同一条M链。</p> 
<p>为了避免这种重复查找&#xff0c;我们可以增加判断&#xff1a;</p> 
<p>如果当前M点的</p> 
<ul><li>左上角点是M&#xff0c;则反对角线不用查找了</li><li>右上角点是M&#xff0c;则正对角线不用查找了</li><li>上边点是M&#xff0c;则垂直线不用查找了</li><li>左边点是M&#xff0c;最水平线不用查找了</li></ul> 
<p><img alt="" height="165" src="https://img-blog.csdnimg.cn/5a8e34a2a521471d8c7a8baa90d194b8.png" width="249" /></p> 
<p>如上图红色M的左上、上、左点都是M&#xff0c;因此红色M的</p> 
<ul><li>反对角线已经被其左上点M查找过了&#xff0c;因此不用找了&#xff0c; </li><li>垂直线已经被其上边点M查找过了&#xff0c;因此不用找了</li><li>水平线已经被其左边点M查找过了&#xff0c;因此不用找了</li></ul> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Scanner;

public class Main {
  static int n;
  static int m;
  static String[][] matrix;

  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in).useDelimiter(&#34;[,\n]&#34;);

    n &#61; sc.nextInt();
    m &#61; sc.nextInt();

    matrix &#61; new String[n][m];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      for (int j &#61; 0; j &lt; m; j&#43;&#43;) {
        matrix[i][j] &#61; sc.next();
      }
    }

    System.out.println(getResult());
  }

  public static int getResult() {
    int ans &#61; 0;

    int[][] offsets &#61; {<!-- -->{0, 1}, {1, 0}, {1, 1}, {1, -1}};

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      for (int j &#61; 0; j &lt; m; j&#43;&#43;) {
        if (&#34;M&#34;.equals(matrix[i][j])) {
          for (int[] offset : offsets) {
            int oldI &#61; i - offset[0];
            int oldJ &#61; j - offset[1];

            if (oldI &gt;&#61; 0 &amp;&amp; oldI &lt; n &amp;&amp; oldJ &gt;&#61; 0 &amp;&amp; oldJ &lt; m &amp;&amp; &#34;M&#34;.equals(matrix[oldI][oldJ])) {
              continue;
            }

            int len &#61; 1;
            int newI &#61; i &#43; offset[0];
            int newJ &#61; j &#43; offset[1];

            while (newI &gt;&#61; 0
                &amp;&amp; newI &lt; n
                &amp;&amp; newJ &gt;&#61; 0
                &amp;&amp; newJ &lt; m
                &amp;&amp; &#34;M&#34;.equals(matrix[newI][newJ])) {
              len&#43;&#43;;
              newI &#43;&#61; offset[0];
              newJ &#43;&#61; offset[1];
            }

            ans &#61; Math.max(ans, len);
          }
        }
      }
    }

    return ans;
  }
}
</code></pre> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
let n, m;
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 1) {
    [n, m] &#61; lines[0].split(&#34;,&#34;).map(Number);
  }

  if (n &amp;&amp; lines.length &#61;&#61;&#61; n &#43; 1) {
    lines.shift();
    const matrix &#61; lines.map((line) &#61;&gt; line.split(&#34;,&#34;));

    console.log(getResult(matrix, n, m));

    lines.length &#61; 0;
  }
});

function getResult(matrix, n, m) {
  let ans &#61; 0;

  const offsets &#61; [
    [0, 1],
    [1, 0],
    [1, 1],
    [1, -1],
  ];

  for (let i &#61; 0; i &lt; n; i&#43;&#43;) {
    for (let j &#61; 0; j &lt; m; j&#43;&#43;) {
      if (matrix[i][j] &#61;&#61; &#34;M&#34;) {
        for (let offset of offsets) {
          const oldI &#61; i - offset[0];
          const oldJ &#61; j - offset[1];

          if (
            oldI &gt;&#61; 0 &amp;&amp;
            oldI &lt; n &amp;&amp;
            oldJ &gt;&#61; 0 &amp;&amp;
            oldJ &lt; m &amp;&amp;
            matrix[oldI][oldJ] &#61;&#61; &#34;M&#34;
          ) {
            continue;
          }

          let len &#61; 1;
          let newI &#61; i &#43; offset[0];
          let newJ &#61; j &#43; offset[1];

          while (
            newI &gt;&#61; 0 &amp;&amp;
            newI &lt; n &amp;&amp;
            newJ &gt;&#61; 0 &amp;&amp;
            newJ &lt; m &amp;&amp;
            matrix[newI][newJ] &#61;&#61; &#34;M&#34;
          ) {
            len&#43;&#43;;
            newI &#43;&#61; offset[0];
            newJ &#43;&#61; offset[1];
          }

          ans &#61; Math.max(ans, len);
        }
      }
    }
  }

  return ans;
}
</code></pre> 
<p></p> 
<h4 style="background-color:transparent;">Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n, m &#61; map(int, input().split(&#34;,&#34;))
matrix &#61; [input().split(&#34;,&#34;) for _ in range(n)]


# 算法入口
def getResult():
    ans &#61; 0

    offsets &#61; ((0, 1), (1, 0), (1, 1), (1, -1))

    for i in range(n):
        for j in range(m):
            if matrix[i][j] &#61;&#61; &#34;M&#34;:
                for offset in offsets:
                    oldI &#61; i - offset[0]
                    oldJ &#61; j - offset[1]

                    if n &gt; oldI &gt;&#61; 0 and m &gt; oldJ &gt;&#61; 0 and matrix[oldI][oldJ] &#61;&#61; &#34;M&#34;:
                        continue

                    length &#61; 1
                    newI &#61; i &#43; offset[0]
                    newJ &#61; j &#43; offset[1]

                    while n &gt; newI &gt;&#61; 0 and m &gt; newJ &gt;&#61; 0 and matrix[newI][newJ] &#61;&#61; &#34;M&#34;:
                        length &#43;&#61; 1
                        newI &#43;&#61; offset[0]
                        newJ &#43;&#61; offset[1]

                    ans &#61; max(ans, length)

    return ans


# 调用算法
print(getResult())
</code></pre> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX(a,b) ((a) &gt; (b) ? (a) : (b))

int main() {
    int n, m;
    scanf(&#34;%d,%d\n&#34;, &amp;n, &amp;m);

    char matrix[n][m];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
        for (int j &#61; 0; j &lt; m; j&#43;&#43;) {
            scanf(&#34;%c&#34;, &amp;matrix[i][j]);
            getchar();
        }
    }

    int ans &#61; 0;

    int offsets[4][2] &#61; {<!-- -->{0, 1},
                         {1, 0},
                         {1, 1},
                         {1, -1}};

    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
        for (int j &#61; 0; j &lt; m; j&#43;&#43;) {
            if (matrix[i][j] &#61;&#61; &#39;M&#39;) {
                for (int k &#61; 0; k &lt; 4; k&#43;&#43;) {
                    int offsetI &#61; offsets[k][0];
                    int offsetJ &#61; offsets[k][1];

                    int oldI &#61; i - offsetI;
                    int oldJ &#61; j - offsetJ;

                    if(oldI &gt;&#61; 0 &amp;&amp; oldI &lt; n &amp;&amp; oldJ &gt;&#61; 0 &amp;&amp; oldJ &lt; m &amp;&amp; matrix[oldI][oldJ] &#61;&#61; &#39;M&#39;) {
                        continue;
                    }

                    int len &#61; 1;
                    int newI &#61; i &#43; offsetI;
                    int newJ &#61; j &#43; offsetJ;

                    while (newI &gt;&#61; 0 &amp;&amp; newI &lt; n &amp;&amp; newJ &gt;&#61; 0 &amp;&amp; newJ &lt; m &amp;&amp; matrix[newI][newJ] &#61;&#61; &#39;M&#39;) {
                        len&#43;&#43;;
                        newI &#43;&#61; offsetI;
                        newJ &#43;&#61; offsetJ;
                    }

                    ans &#61; MAX(ans, len);
                }
            }
        }
    }

    printf(&#34;%d\n&#34;, ans);

    return 0;
}</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>